﻿module Sustainalytics.MostRecentActivity {

    import Common = Sustainalytics.Common;

    export class MostRecentActivityService implements IMostRecentActivityService {

        private baseUrl: string = '/mostrecentactivityserv/api/v1/MostRecentActivity';

        constructor(
            private $q: ng.IQService,
            private $http: ng.IHttpService) {
        }

        public get(listOptions: Common.ListOptions): ng.IHttpPromise<any> {
            var ctx = this;

            var params = $.param(listOptions ? { listOptions: listOptions } : {});

            var promise = ctx.$http.get(ctx.baseUrl + '?' + params);
            return promise;
        }

        public getList(skip: number, take: number): ng.IPromise<MostRecentActivityDto[]> {
            var ctx = this;

            var def = ctx.$q.defer();

            var listOptions = new Common.ListOptions();
            listOptions.skip = skip;
            listOptions.take = take;

            var promise = ctx.get(listOptions);

            promise.then(
                // success
                function (res) {
                    def.resolve(res.data);
                },
                // err
                function (res) {
                    def.reject(res);
                });

            return def.promise;
        }

        public saveMostRecentActivity(companyId: string): ng.IPromise<any> {
            var ctx = this;
            var def = this.$q.defer();

            var url: string = ctx.baseUrl + '/' + companyId;

            var promise: ng.IHttpPromise<{}> = this.$http.post(url, null);

            promise.success(function (data) {
                def.resolve(data);
            })
                .error(function (data, status, headers, config) {
                    def.reject({ data: data, status: status, headers: headers, config: config });
                })
            return def.promise;
        }

    }

    export var mostRecentActivityServiceName = 'MostRecentActivityService';

    angular.module(mostRecentActivityModuleName).service(mostRecentActivityServiceName, MostRecentActivityService);
} 